# -*- coding:utf-8 -*-
# @Time : 21-9-2 下午3:26 
# @Author : zdy
# @File : UIMethod.py
# desc:

from UI.Method import Ui_Method
from Class.cs598 import cs_598
import os, glob
from Class.Format import Serialization
from importQT import *

class UIMethod(QDialog, Ui_Method):
    def __init__(self):
        super(UIMethod, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("方法")
        self.applyResource()
        self.btnSave.clicked.connect(self.saveParams)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.btnload.clicked.connect(self.loadMethod)
        self.btndel.clicked.connect(self.delMethod)
        self.loadfilename()
        self.cbName.setEditable(True)
        self.updateParams(cs_598.config)

    def loadfilename(self):
        """cbName加载方法名"""
        self.cbName.clear()
        os.chdir(cs_598.configFolder)
        for file in glob.glob("*.bin"):
            name = file.split(".")[0]
            if name != "systemconfig":
                self.cbName.addItem(name)
        self.cbName.setCurrentText(cs_598.config.name)

    def applyResource(self):
        icon = QIcon()
        icon.addPixmap(QPixmap(":/pic/icon.ico"), QIcon.Normal, QIcon.On)
        self.setWindowIcon(icon)

    def loadMethod(self):
        config = cs_598.getconfig(self.cbName.currentText())
        if config is not None:
            cs_598.config = config
            self.updateParams(cs_598.config)

    def updateParams(self, config):
        self.cbName.setCurrentText(config.name)
        self.spinBoxBsaselineV.setValue(config.baselineDeviation)
        self.spinBoxBaselineT.setValue(config.baselineTime)
        self.spinBoxSwitch1.setValue(config.switch1)
        self.spinBoxSwitch2.setValue(config.switch2)
        self.dSpinBoxBlank1.setValue(config.blank1)
        self.dSpinBoxBlank2.setValue(config.blank2)
        self.spinBoxPurgeT.setValue(config.timePurge)
        self.spinBoxLance.setValue(config.timeLance)
        self.spinBoxChamberT.setValue(config.timeChamber)
        self.spinBoxPowerT.setValue(config.powerDelay)
        self.spinBoxPower.setValue(config.powerUplimit)
        self.spinBoxWaiting.setValue(config.waitingT)
        self.spinBoxDecimals.setValue(config.decimals)
        self.chkAutoSaving.setChecked(config.autoSaving)

        self.chk0.setChecked(config.channels[0].enable)
        self.chk1.setChecked(config.channels[1].enable)
        self.chk2.setChecked(config.channels[2].enable)
        self.chk3.setChecked(config.channels[3].enable)

        self.dSpinBoxPeakratio0.setValue(config.channels[0].ratio)
        self.dSpinBoxPeakratio1.setValue(config.channels[1].ratio)
        self.dSpinBoxPeakratio2.setValue(config.channels[2].ratio)
        self.dSpinBoxPeakratio3.setValue(config.channels[3].ratio)

        self.spinBoxComporator0.setValue(config.channels[0].comporator)
        self.spinBoxComporator1.setValue(config.channels[1].comporator)
        self.spinBoxComporator2.setValue(config.channels[2].comporator)
        self.spinBoxComporator3.setValue(config.channels[3].comporator)

        self.spinBoxDelay0.setValue(config.channels[0].delay)
        self.spinBoxDelay1.setValue(config.channels[1].delay)
        self.spinBoxDelay2.setValue(config.channels[2].delay)
        self.spinBoxDelay3.setValue(config.channels[3].delay)

        self.spinBoxMin0.setValue(config.channels[0].min)
        self.spinBoxMin1.setValue(config.channels[1].min)
        self.spinBoxMin2.setValue(config.channels[2].min)
        self.spinBoxMin3.setValue(config.channels[3].min)

        self.spinBoxMax0.setValue(config.channels[0].max)
        self.spinBoxMax1.setValue(config.channels[1].max)
        self.spinBoxMax2.setValue(config.channels[2].max)
        self.spinBoxMax3.setValue(config.channels[3].max)

    def saveParams(self):
        config = cs_598.config
        config.name = self.cbName.currentText()
        config.baselineDeviation = self.spinBoxBsaselineV.value()
        config.baselineTime = self.spinBoxBaselineT.value()
        config.switch1 = self.spinBoxSwitch1.value()
        config.switch2 = self.spinBoxSwitch2.value()
        config.blank1 = self.dSpinBoxBlank1.value()
        config.blank2 = self.dSpinBoxBlank2.value()
        config.factors[0].blank = config.blank1
        config.factors[1].blank = config.blank1
        config.factors[2].blank = config.blank2
        config.factors[3].blank = config.blank2
        config.timePurge = self.spinBoxPurgeT.value()
        config.timeLance = self.spinBoxLance.value()
        config.timeChamber = self.spinBoxChamberT.value()
        config.powerDelay = self.spinBoxPowerT.value()
        config.powerUplimit = self.spinBoxPower.value()
        config.waitingT = self.spinBoxWaiting.value()
        config.decimals = self.spinBoxDecimals.value()
        config.autoSaving = self.chkAutoSaving.checkState() == Qt.Checked

        config.channels[0].enable = self.chk0.checkState() == Qt.Checked
        config.channels[1].enable = self.chk1.checkState() == Qt.Checked
        config.channels[2].enable = self.chk2.checkState() == Qt.Checked
        config.channels[3].enable = self.chk3.checkState() == Qt.Checked

        config.channels[0].ratio = self.dSpinBoxPeakratio0.value()
        config.channels[1].ratio = self.dSpinBoxPeakratio1.value()
        config.channels[2].ratio = self.dSpinBoxPeakratio2.value()
        config.channels[3].ratio = self.dSpinBoxPeakratio3.value()

        config.channels[0].comporator = self.spinBoxComporator0.value()
        config.channels[1].comporator = self.spinBoxComporator1.value()
        config.channels[2].comporator = self.spinBoxComporator2.value()
        config.channels[3].comporator = self.spinBoxComporator3.value()

        config.channels[0].delay = self.spinBoxDelay0.value()
        config.channels[1].delay = self.spinBoxDelay1.value()
        config.channels[2].delay = self.spinBoxDelay2.value()
        config.channels[3].delay = self.spinBoxDelay3.value()

        config.channels[0].min = self.spinBoxMin0.value()
        config.channels[1].min = self.spinBoxMin1.value()
        config.channels[2].min = self.spinBoxMin2.value()
        config.channels[3].min = self.spinBoxMin3.value()

        config.channels[0].max = self.spinBoxMax0.value()
        config.channels[1].max = self.spinBoxMax1.value()
        config.channels[2].max = self.spinBoxMax2.value()
        config.channels[3].max = self.spinBoxMax3.value()

        cs_598.deinit()
        self.loadfilename()

    def delMethod(self):
        if self.cbName.currentText() == cs_598.config.name:
            QMessageBox.warning(self,"提示", "不可删除正在使用的方法")
            return
        filepath = os.path.join(cs_598.configFolder, self.cbName.currentText() + ".bin")
        if os.path.exists(filepath):
            os.remove(filepath)
        self.loadfilename()
